package com.px.user.mapper;

import com.px.user.entity.Avatar;
import com.px.user.entity.User;
import org.apache.ibatis.annotations.*;
import org.springframework.stereotype.Repository;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;
import java.util.Map;

@Repository
public interface UserMapper {

    @Options(useGeneratedKeys = true, keyProperty = "id", keyColumn = "id")
    @Insert("insert into user (username,intro,roles,isRobot,isVirtual,phone,password,money,sex,nickname,avatar," +
            "disabled,source,ip,inviteRoomId,name,bankCard,bankName) values (#{username},#{intro}," +
            "#{roles},#{isRobot},#{isVirtual},#{phone},#{password},#{money},#{sex},#{nickname},#{avatar},#{disabled}," +
            "#{source},#{ip},#{inviteRoomId},#{name},#{bankCard},#{bankName})")
    void insert(User user);

    @Update("update user set username=ifnull(#{username}, username),roles=ifnull(#{roles},roles)," +
            "phone=ifnull(#{phone},phone),password=ifnull(#{password},password)," +
            "money=ifnull(#{money},money),sex=ifnull(#{sex},sex)," +
            "nickname=ifnull(#{nickname},nickname),avatar=ifnull(#{avatar},avatar)," +
            "disabled=ifnull(#{disabled},disabled),intro=ifnull(#{intro},intro)," +
            "isRobot=ifnull(#{isRobot},isRobot),ip=ifnull(#{ip},ip)," +
            "inviteRoomId=ifnull(#{inviteRoomId},inviteRoomId),name=ifnull(#{name},name)," +
            "bankCard=ifnull(#{bankCard},bankCard),bankName=ifnull(#{bankName},bankName)," +
            "isVirtual=ifnull(#{isVirtual},isVirtual) " +
            "where id=#{id}")
    int update(User user);

    /**
     * 检查房管是否可以禁用此用户
     * @param map
     * @return
     */
    @Select("select user.id from room,user where room.adminId=#{adminId} and " +
            "room.id=user.inviteRoomId and user.id=#{userId} limit 1")
    Long disableCheck(Map map);

    @Update("update user set disabled=ifnull(#{disabled},disabled) where id=#{id}")
    void updateDisable(User user);

    @Update("update user set password=ifnull(#{password},password) where username=#{username}")
    void updatePassword(User user);

    @Delete("delete from user where id =#{id}")
    void delete(Long id);

    @Select("select * from user where id=#{id}")
    User findById(Long id);

    @Select("select password from user where username=#{username}")
    String findByUsername(String username);

    @Select("select nickname from user where id=#{id}")
    String findNicknameById(Long id);

    @Transactional
    @Update("update user set money=ifnull(money, 0)+ifnull(#{money},0) where id=#{id}")
    void addMoney(User user);

    @Select({"<script>",
            "select id,avatar",
            "from user",
            "where id in",
            "<foreach item='i' collection='ids'",
            "open='(' separator=',' close=')'>",
            "#{i}",
            "</foreach>",
            "</script>"})
    List<User> findAvatars(@Param("ids") List ids);

    @Select({"<script>",
            "select * from user",
            "<where>",
            " <if test='id != null'>and id = #{id}</if>",
            " <if test='isRobot != null'>and isRobot = #{isRobot}</if>",
            " <if test='isVirtual != null'>and isVirtual = #{isVirtual}</if>",
            " <if test='disabled != null'>and disabled = #{disabled}</if>",
            " <if test='qstart != null'>and createTime >= #{qstart}</if>",
            " <if test='qend != null'>and createTime <![CDATA[<=]]> #{qend}</if>",
            " <if test='qrole != null'>and json_search(roles, 'one', #{qrole}) is not null</if>",
            " <if test='username != null'>and username like #{username}</if>",
            " <if test='nickname != null'>and nickname like #{nickname}</if>",
            "</where>",
            "order by ${orderField} desc limit #{startIndex},#{limit}",
            "</script>"})
    List<User> paging(User user);

    @Select({"<script>",
            "select count(1) from user",
            "<where>",
            " <if test='id != null'>and id = #{id}</if>",
            " <if test='isRobot != null'>and isRobot = #{isRobot}</if>",
            " <if test='isVirtual != null'>and isVirtual = #{isVirtual}</if>",
            " <if test='disabled != null'>and disabled = #{disabled}</if>",
            " <if test='qstart != null'>and createTime >= #{qstart}</if>",
            " <if test='qend != null'>and createTime <![CDATA[<=]]> #{qend}</if>",
            " <if test='qrole != null'>and json_search(roles, 'one', #{qrole}) is not null</if>",
            " <if test='username != null'>and username like #{username}</if>",
            " <if test='nickname != null'>and nickname like #{nickname}</if>",
            "</where>",
            "</script>"})
    Integer pagingCount(User user);

    @Select("select * from user where id=#{teacherId} and json_search(roles, 'one', 'TEACHER') is not null")
    User findByTeacherId(Long teacherId);

    @Select("select id from user where ip=#{ip}")
    List<Long> findByIp(String ip);

    /**
     * 机器人
     */
    @Select("select count(1) from user where isRobot=1")
    Integer robotCount();

    @Delete("delete from user where isRobot=1")
    void deleteRobots();

    @Select("select * from user where isRobot=1 order by rand() limit #{limit}")
    List<User> findRandomRobot(Integer limit);

    @Select("select id from user where isRobot=1 order by rand() limit #{limit}")
    List<Long> findIdRandomRobot(Integer limit);


    /**
     * 头像管理
     *
     * @return
     */
    @Select("select * from avatar")
    List<Avatar> findAllAvatar();

    @Insert("insert into avatar (url) values (#{url})")
    void insertAvatar(Avatar avatar);

    @Update("update avatar set url=ifnull(#{url}, url) where id=#{id}")
    void updateAvatarById(Avatar avatar);

    @Delete("delete from avatar where id=#{id}")
    void deleteAvatar(Integer id);

    /**
     * 随机头像
     *
     * @return
     */
    @Select("select url from avatar order by rand() limit 1")
    String randomAvatar();
}
